//
//  ViewController.m
//  Photo
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *photoScrollView;
@property (nonatomic, strong) UIScrollView *smallScrollView;

@property (nonatomic, strong) UIPageControl *page;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.photoScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    self.photoScrollView.contentSize = CGSizeMake(WIDTH * 9, 0);
    self.photoScrollView.delegate = self;
    self.photoScrollView.pagingEnabled = YES;
    self.photoScrollView.bounces = NO;
    [self.view addSubview:self.photoScrollView];
    
    NSArray *photoArr = @[@"u=3808451166,1723806820&fm=23&gp=0", @"u=1019182931,1402276621&fm=23&gp=0",@"u=1712427375,1304261470&fm=23&gp=0", @"u=2019074505,523552158&fm=23&gp=0", @"u=2216889308,3751754546&fm=23&gp=0", @"u=2301975825,1085041863&fm=23&gp=0", @"u=2427941702,1013456056&fm=23&gp=0", @"u=3808451166,1723806820&fm=23&gp=0", @"u=1019182931,1402276621&fm=23&gp=0"];
    for (NSInteger i = 0; i < photoArr.count; i++) {
        self.smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT)];
        self.smallScrollView.delegate = self;
        self.smallScrollView.minimumZoomScale = 0.5;
        self.smallScrollView.maximumZoomScale = 2;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        UIImage *image = [UIImage imageNamed:photoArr[i]];
        [imageView setImage:image];
        [self.smallScrollView addSubview:imageView];
        [self.photoScrollView addSubview:self.smallScrollView];
           }
    self.photoScrollView.contentOffset = CGPointMake(WIDTH, 0);
    
     self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(50, HEIGHT - 20, 100, 20)];
    self.page.numberOfPages = 7;
    self.page.currentPage = 0;
    [self.page addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.page];
}
// 点击小点 图片跟着滑动
- (void)pageChange:(UIPageControl *)page {
    [self.photoScrollView setContentOffset:CGPointMake((page.currentPage + 1) * WIDTH, 0) animated:YES];
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    for (UIScrollView *scroll in scrollView.subviews) {
        if ([scroll isKindOfClass:[UIScrollView class]]) {
            scroll.zoomScale = 1;
        }
    }
    self.page.currentPage = self.photoScrollView.contentOffset.x / WIDTH - 1;

    if (self.photoScrollView.contentOffset.x == 8 * WIDTH) {
        self.photoScrollView.contentOffset = CGPointMake(WIDTH, 0);
        self.page.currentPage = 0;
    }
    if (self.photoScrollView.contentOffset.x == 0) {
        self.photoScrollView.contentOffset =CGPointMake(7 * WIDTH, 0);
        self.page.currentPage = 6;
    }
    NSLog(@"%f", self.photoScrollView.contentOffset.x);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
